#include <iostream>
#include <getch.h>
#include "manager_view.h"
#include "manager_data.h"


void ManagerView::menu(void)
{
    ManagerData m;
    m.load();
    
    while(1)
    {
        system("clear");
	    cout << "＊＊＊欢迎使用指针图书馆管理系统＊＊＊" << endl;
	    cout << "[1] 添加管理员" << endl;
	    cout << "[2] 删除管理员" << endl;
	    cout << "[3] 查看管理员" << endl;
	    cout << "[q] 退出登录" << endl;
	    cout << "[0] 退出系统" << endl;
	    cout << "请选择:";
	    char ch = getch();
	    cout << ch << endl; 
	    switch(ch)
	    {
	        case '1': add();break;
	        case '2': del();break;
	        case '3': list();break;
	        case 'q': m.save();return ;
	        case '0': m.save();exit(0);
	    }
    }
}

void ManagerView::add(void)
{
    system("clear");
    int id=0;
    char name[20]={};
    char password1[20] ={};
    char password2[20] ={};
    id = generator_ManagerId();
    cout << "请输入您要添加的管理员姓名:" << endl;
    cin >>name;
    getchar();
    cout << "请输入密码:";
    cin >>password1;
    getchar();
    cout << "请再次输入密码:";
    cin >>password2;
    getchar();
    if(0 != strcmp(password1,password2))
    {
        cout << "两次密码不一致" << endl;
        get_char();
        return;
    }
    ManagerService n;
    if(true == n.addManager(id,name,password1))
    {
        cout << "添加成功" << endl;
    }
    get_char();
}

void ManagerView::del(void)
{
    system("clear");
    int id1 =-1,id2 =-1;
    cout << "请输入您要删除的管理员ID:" << endl;
    cin >> id1;getchar();
    cout  << "请再次输入您要删除的管理员ID:" << endl;
    cin >> id2;getchar();
    if(id1 != id2) 
    {
        cout <<  "前后两次输入不符合" << endl;
        return;
    }
    ManagerService n;
    if(true == n.deleteManager(id1))
    {
        cout << "删除成功!" << endl;
    }
    else
    {
        cout << "此ID号不存在" << endl;
    }
    get_char();
}

void ManagerView::list(void)
{
    system("clear");
    ManagerService n;
    n.listManager();
}
